# 定义字典
person = {
    'name': '小明',
    'age': 20,
    'is_married': False,
    'hobies': ['运动', '钓鱼', '音乐']
}
# print(person, type(person))
# 练习：定义字典保存一个手机的信息，包含品牌型号、颜色、尺寸、价格、是否在售
phone = {
    'brand': '华为Mate70',
    'color': '黑色',
    'size': 6.32,
    'price': None,
    'is_onsale': False
}
# 3.删除
# del phone['size']
# phone.pop('price')
# 清空
phone.clear()
print(phone)
# 2.修改和新增
# 单独操作
# phone['price'] = 5999
# phone['memory'] = '16GB'
# 批量操作
# phone.update({'price': 6999, 'memory': '32GB', 'cpu': '麒麟9300S'})

# 1.访问
# print(phone['brand'], phone.get('brand'))
# print(phone['cpu'])
# get访问的元素不存在返回None，也可以设置默认值
# print(phone.get('cpu', '麒麟9300s'))
# 判断元素是否存在
# print('cpu' in phone)

# 获取所有的键
# dict_keys(['brand', 'color', 'size', 'price', 'is_onsale'])
# print(phone.keys())
# 获取所有的值
# dict_values(['华为Mate70', '黑色', 6.32, None, False])
# print(phone.values())
# 获取所有的键值对
# dict_items([('brand', '华为Mate70'), ('color', '黑色'), ('size', 6.32), ('price', None), ('is_onsale', False)])
# print(phone.items())
# 练习：遍历字典，有哪些方法
# for key in phone:
#     # key 键
#     print(key, phone[key])
# for key in phone.keys():
#     print(key, phone[key])
# for value in phone.values():
#     print(value)
# for item in phone.items():
#     print(item[0], item[1])
"""
# 练习：定义字典，包含一组成绩，计算出总成绩和平均成绩。
nums = {
    'chinese': 92,
    'math': 88,
    'english': 77,
    'history': 93
}
total = 0
for value in nums.values():
    # print(value)
    total += value
print(total, total/len(nums))
"""


